package dk.lector;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import dk.lector.domain.DocFacade;
import dk.lector.providers.DOC_METADATAContentProvider;
import dk.lector.providers.SEARCH_RESULTContentProvider;
import dk.lector.providers.SearchViewContentProvider;
import dk.lector.utils.IconLoader;

public class SearchResultActivity extends BaseFullScreenActivity implements OnItemClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_layout);
		
		String username = "JW7kMeqcT/PygKHCLEUQZNbv+QHvk04CvGB15ElakcVn3F1AJI1dkgslcMyQcAwSMcxYQqe3WQVXuFu6tuGdAIIshpAhbtzHcM3ChwYPHS8z9Ybl0GAEBsBdB+OboXQCX2kq35+ehCJKHyfkGRrZR5DTUUne+NoyJ1lyMOcnnQkjWpPQ6GNaZ1n3rjgXo6+OTUJCFpht1RxaNz4TP2DoYvkvNn+uSznXQiBIdNtAGUXExXYVlhlaFQHHF2jCQ4LEIibSQDSwe5H6s+AIpWLBy+ZSTJnUQyQLBAIkiAIKZM/+4N2w2e5kElX3u7C7BZJpRkuv5PPNhQ0ENH5A/Mb3MQ==";
		String password = "jk288EjhRo1NboT9AxozyMJv2NqC8e1i7s8KMt2Yu7OeSpBf9tUnU+t2fH+StjLpFPLgrxUnvEHb5QKy4IkblRzPC0SZz7kLtZrBJuiXVvOsonB7MVOPP8Wg/iyViWiIb3p6+i8XwRu1tO0/7kAs+k8ZovMqHtsUcjK+xJlNJ8zgLHNgfY0wh4eEzL1arB1DV4cCW7DiDLaCSTTlxHxFbdppWEId8+qUXYy9Or26nZ9z1TeaN1mcluzofLHleHMxvwHVUpVFR5VXJqvNSxiixx01L2yQh5k+WkBJY8fNktXXhhx9Dw1cUEH4zqd2ckl/KxMyw8b3qJENXYWiXOtXgQ==";
		
		Cursor resultCursor = DocFacade.searchDocumentsByUri(this, getIntent().getData(), username, password);
		resultCursor.setNotificationUri(getContentResolver(), SEARCH_RESULTContentProvider.CONTENT_URI);
		
		startManagingCursor(resultCursor);
		
		String[] from = new String[] { SearchViewContentProvider._ID,
				SearchViewContentProvider.TITLE};
		int[] to = new int[] { R.id.searchresult_item_id,
				R.id.searchresult_item_title};
		
		ListAdapter adapter = new SimpleCursorAdapter(
				SearchResultActivity.this, R.layout.doclist_layout_entry,
				resultCursor, from, to) {
			
			@Override
			public void bindView(View view, Context context, Cursor cursor) {
				super.bindView(view, context, cursor);

				int icon = cursor.getInt(cursor
						.getColumnIndex(SearchViewContentProvider.ICON_ID));
				ImageView image = (ImageView) view
						.findViewById(R.id.imageresult_item_id);
				image.setImageResource(IconLoader.parseType(
						icon, SearchResultActivity.this));
			}
		};
		ListView list = ((ListView) findViewById(R.id.doc_listView));
		list.setAdapter(adapter);
		list.setClickable(true);
		list.setOnItemClickListener(this);
	}

	@Override
	public String actionBarText() {
		return getString(R.string.documents_result_activity_text);
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		TextView tv = (TextView) arg1.findViewById(R.id.searchresult_item_id);
		Log.d("ONCLICK", "" + Integer.parseInt((String) tv.getText()));
		Intent getDoc = new Intent(this, DocumentLoaderActivity.class);
		getDoc.putExtra(DOC_METADATAContentProvider._ID, Integer.parseInt((String) tv.getText()));
		startActivity(getDoc);	
		
	}
    
}
